﻿using Base.FileRemoteModel.UserFile.Model;
using Basic.FlowGatewayModule.Interface;
using Basic.FlowGatewayModule.Model;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.Application;
using Basic.FlowRemoteModel.Application.Model;
using Basic.FlowRemoteModel.FlowAudit;
using Basic.FlowRemoteModel.FlowAudit.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Modular;

namespace Basic.FlowGatewayModule.lmpl
{
    internal class FlowAuditService : IFlowAuditService
    {
        private readonly IUserFileService _Service;
        private readonly IEmpService _Emp;
        public FlowAuditService ( IUserFileService service, IEmpService emp )
        {
            this._Emp = emp;
            this._Service = service;
        }

        public CreateAuditResult BeginAudit ( BeginFlowArg arg, IUserState state )
        {
            return new BeginAudit
            {
                FlowArg = arg,
                User = new FlowUser
                {
                    CompanyId = state.ToCompanyId(),
                    EmpId = state.ToEmpId(),
                    DeptId = state.ToDeptId(),
                    UnitId = state.ToUnitId()
                }
            }.Send();
        }
        public FlowFile[] GetFiles ( long logId )
        {
            FlowLogItem[] items = new GetFlowLogItems
            {
                LogId = logId
            }.Send();
            if ( items.IsNull() )
            {
                return Array.Empty<FlowFile>();
            }
            UserFileGetParam param = new UserFileGetParam
            {
                LinkPk = items.ConvertAll(a => a.Id),
                LinkType = ConstDic.FlowSignLinkType,
                Extension = ConstDic.PdfConvertExtension
            };
            UserFileDatum[] list = this._Service.GetFiles(param);
            Dictionary<long, string> emp = this._Emp.GetEmpName(items.ConvertAll(a => a.EmpId));
            return list.ConvertAll(a =>
            {
                FlowLogItem item = items.Find(c => c.Id == a.LinkBizPk);
                return new FlowFile
                {
                    FileId = a.Id,
                    LinkBizPk = a.LinkBizPk,
                    FileName = a.FileName,
                    FileUri = a.FileUri,
                    UpEmpId = item.EmpId,
                    NodeName = item.NodeName,
                    Extension = a.Extension,
                    UpEmp = emp.GetValueOrDefault(item.EmpId)
                };
            });
        }
        public FlowAuditLog GetAuditLog ( long logId )
        {
            return new GetAuditLog
            {
                LogId = logId
            }.Send();
        }
        public FlowNodeState[] GetNodeState ( long logId, long flowId )
        {
            return new GetAuditNodeState
            {
                FlowId = flowId,
                LogId = logId
            }.Send();
        }
        public AuditNodeLog GetCurentNode ( long id, long empId )
        {
            return new GetCurentNode
            {
                Id = id,
                EmpId = empId
            }.Send();
        }
        public NextStepResult GetNextStep ( long id )
        {
            return new GetNextStep
            {
                Id = id
            }.Send();
        }
        public FlowAuditItemStatus Submit ( long id, AuditRes res )
        {
            return new SubmitAuditResult
            {
                LogId = id,
                Result = res
            }.Send();
        }

        public CopyNodeLog GetCopyLog ( long id, long empId )
        {
            return new GetCopyNodeLog
            {
                Id = id,
                EmpId = empId
            }.Send();
        }
    }
}
